var validator;
var $typeAddForm = $("#type-add-form");
var $typeName = $typeAddForm.find("input[name='typeName']");
var $icon = $typeAddForm.find("input[name='icon']");
var $icon_drop = $typeAddForm.find("div.icon-drop");

$(function() {
    $icon_drop.hide();
    validateRule();
    createTypeTree();
    
    $typeAddForm.find("input[name='icon']").focus(function() {
        $icon_drop.show();
    });

    $("#type-add").click(function(event) {
        var obj = event.srcElement || event.target;
        if (!$(obj).is("input[name='icon']")) {
            $icon_drop.hide();
        }
    });

    $icon_drop.find(".type-icon .col-sm-6").on("click", function() {
        var icon = "zmdi " + $(this).find("i").attr("class").split(" ")[1];
        $icon.val(icon);
    });

    $("#type-add .btn-save").click(function() {
        var name = $(this).attr("name");
        getType();
        validator = $typeAddForm.validate();
        var flag = validator.form();
        if (flag) {
            if (name == "save") {
                $.post(ctx + "shopping/type/add", $typeAddForm.serialize(), function(r) {
                    if (r.code == 0) {
                        refresh();
                        closeModal();
                        $MB.n_success(r.msg);
                    } else $MB.n_danger(r.msg, '.modal');
                });
            }
            if (name == "update") {
                $.post(ctx + "shopping/type/update", $typeAddForm.serialize(), function(r) {
                    if (r.code == 0) {
                        refresh();
                        closeModal();
                        $MB.n_success(r.msg);
                    } else $MB.n_danger(r.msg, '.modal');
                });
            }
        }
    });

    $("#type-add .btn-close").click(function() {
        $("#type-add-modal-title").html('新增类型');
        $("input:radio[value='0']").trigger("click");
        closeModal();
    });

});

function closeModal() {
    $icon.val("").parents(".row").show();
	$("#type-add-button").attr("name", "save");
    $MB.closeAndRestModal("type-add");
    validator.resetForm();
    $MB.refreshJsTree("typeTree", createTypeTree());
}

function validateRule() {
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    validator = $typeAddForm.validate({
        rules: {
            typeName: {
                required: true,
                minlength: 1,
                maxlength: 20,
                remote: {
                    url: "shopping/type/checkTypeName",
                    type: "get",
                    dataType: "json",
                    data: {
                        typeName: function() {
                            return $("input[name='typeName']").val().trim();
                        },
                        oldTypeName: function() {
                            return $("input[name='oldTypeName']").val().trim();
                        },
                        type: function() {
                            return $("input[name='type']").val();
                        }
                    }
                }
            }
        },
        messages: {
            typeName: {
                required: icon + "请输入名称",
                minlength: icon + "名称长度1到20个字符",
                remote: icon + "该名称已经存在"
            }
        }
    });
}

function createTypeTree() {
    $.post(ctx + "shopping/type/tree", {}, function(r) {
        if (r.code == 0) {
            var data = r.msg;
            $('#typeTree').jstree({
                "core": {
                    'data': data.children,
                    'multiple': false
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $MB.n_danger(r.msg);
        }
    })

}

function getType() {
    var ref = $('#typeTree').jstree(true);
    $("[name='parentId']").val(ref.get_checked()[0]);
}